<?php

namespace common\model;

use think\Hook;
use think\model\RelationModel;

class OrderTaskFileListModel extends RelationModel
{
    //自动验证
    protected $_validate = [
        ['is_download', ['yes', 'no'], '', self::EXISTS_VALIDATE, 'in'], // 任务类型
        ['from_tenant_id', '', '', self::EXISTS_VALIDATE, 'integer'], // 租户id
        ['to_tenant_id', '', '', self::EXISTS_VALIDATE, 'integer'], // 租户id
        ['from_project_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['entity_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['to_project_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['task_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['step_category_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['step_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['task_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['version_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['order_id,from_tenant_id,to_tenant_id,task_id,version_id', '', '', self::MUST_VALIDATE, 'unique'],

    ];

    //自动完成
    protected $_auto = [
        ['created_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['updated', 'time', self::MODEL_BOTH, 'function'],
    ];


    /**
     * 重载 after insert
     * @param $pk
     * @param $pkName
     * @param $data
     * @param $options
     */
    public function _after_insert($pk, $pkName, $data, $options)
    {
        parent::_after_insert($pk, $pkName, $data, $options);
        if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){
            // 禁用db后处理事件
            return;
        }

        $param = [
            'operate' => 'create',
            'primary_id' => $pk,
            'primary_field' => $pkName,
            'data' => $data,
            'param' => $options,
            'table' => $this->getTableName()
        ];
        Hook::listen('after_order_task_file_list_created', $param);
    }

    /**
     * 更新触发
     * @param $result
     * @param $pkName
     * @param $data
     * @param $options
     * @param $writeEvent
     */
    public function _after_update($result, $pkName, $data, $options, $writeEvent)
    {
        parent::_after_update($result, $pkName, $data, $options, $writeEvent);
        if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){
            // 禁用db后处理事件
            return;
        }

        $param = [
            'level' => 'info',
            'operate' => 'update',
            'primary_id' => $this->oldUpdateKey,
            'primary_field' => $pkName,
            'data' => ["old" => $this->oldUpdateData, "new" => $this->newUpdateData],
            'param' => $options,
            'table' => $this->getTableName()
        ];
        Hook::listen('after_order_task_file_list_updated', $param);
    }

}
